package com.codeing.demo.lru;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shilei
 * @create 2021-08-18 12:32
 * map 负责查找 构建一个虚拟的双向链表 它里面安装的就是一个个 Node 节点 作为数据载体
 */
@Data
public class LruCacheDemo {

    /**
     * 1、构建一个 Node 节点 作为数据的载体
     *
     * @param <K> key
     * @param <V> value
     */
    class Node<K, V> {
        K key;
        V value;
        Node<K, V> prev;
        Node<K, V> next;

        public Node() {
            this.prev = this.next = null;
        }

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
            this.prev = this.next = null;
        }

        @Override
        public String toString() {
            return key + "\t" + value;
        }
    }

    /**
     * 2、构建一个虚拟的双向链表 里面装的就是我们的 Node
     *
     * @param <K>
     * @param <V>
     */
    @Data
    class DoubleLinkedList<K, V> {
        Node<K, V> head;
        Node<K, V> tail;

        //2.1 构造方法
        public DoubleLinkedList() {
            head = new Node<>();
            tail = new Node<>();
            head.next = tail;
            tail.prev = head;
        }

        //2.2 添加到头

        public void addHead(Node<K, V> node) {
            node.next = head.next;
            node.prev = head;
            head.next.prev = node;
            head.next = node;
        }

        //2.3 删除节点
        public void removeNode(Node<K, V> node) {
            node.next.prev = node.prev;
            node.prev.next = node.next;
            node.prev = null;
            node.next = null;
        }

        //2.4 获取最后一个节点
        public Node<K, V> getLastNode() {
            return tail.prev;
        }
    }

    /**
     * 缓存长度
     */
    private Integer cacheSize;
    /**
     * 用来查询数据 map
     */
    private Map<Integer, Node<Integer, Integer>> map;
    /**
     * 用来存储数据 node 节点
     */
    private DoubleLinkedList<Integer, Integer> doubleLinkedList;

    /**
     * 指定缓存长度构造器
     *
     * @param cacheSize 缓存长度
     */
    public LruCacheDemo(Integer cacheSize) {
        this.cacheSize = cacheSize;
        doubleLinkedList = new DoubleLinkedList<>();
        map = new HashMap<>();
    }

    /**
     * 根据当前 key 获取 value
     *
     * @param key key
     * @return 返回 value
     */
    public int get(Integer key) {
        if (!map.containsKey(key)) {
            return -1;
        }
        // 查一遍要把当前 key 移到队首
        Node<Integer, Integer> node = map.get(key);
        doubleLinkedList.removeNode(node);
        doubleLinkedList.addHead(node);
        return node.value;
    }

    /**
     * 添加方法
     *
     * @param key   key
     * @param value value
     */
    public void put(Integer key, Integer value) {
        //包含的话是更新
        if (map.containsKey(key)) {
            //更新 map 中的值 并且双向链表去做移除添加
            Node<Integer, Integer> node = map.get(key);
            node.value = value;
            map.put(key, node);
            doubleLinkedList.removeNode(node);
            doubleLinkedList.addHead(node);
        } else {
            // 如果缓存满了 删除最后一个节点
            if (map.size() == cacheSize) {
                Node<Integer, Integer> lastNode = doubleLinkedList.getLastNode();
                map.remove(lastNode.key);
                doubleLinkedList.removeNode(lastNode);
            }
            //新增节点
            Node<Integer, Integer> newNode = new Node<>(key, value);
            map.put(key, newNode);
            doubleLinkedList.addHead(newNode);
        }
    }

    public static void main(String[] args) {
        LruCacheDemo lruCacheDemo = new LruCacheDemo(3);
        lruCacheDemo.put(1,1);
        lruCacheDemo.put(2,2);
        lruCacheDemo.put(3,3);
        System.out.println(lruCacheDemo.getMap());
        lruCacheDemo.put(4,4);
        System.out.println(lruCacheDemo.getMap());
        System.out.println(lruCacheDemo.get(3));
        System.out.println(lruCacheDemo.getMap());
        lruCacheDemo.put(5,5);
        System.out.println(lruCacheDemo.getMap());

    }
}
